package com.woniuxy.threeproject.end_water_project_user.controller;


import com.woniuxy.threeproject.end_water_project_user.commons.AccountAndWaterMeter;
import com.woniuxy.threeproject.end_water_project_user.commons.Result;
import com.woniuxy.threeproject.end_water_project_user.commons.UUIDUtils;
import com.woniuxy.threeproject.end_water_project_user.commons.UserChekeUtile;
import com.woniuxy.threeproject.end_water_project_user.dao.UserMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Account;
import com.woniuxy.threeproject.end_water_project_user.domain.User;
import com.woniuxy.threeproject.end_water_project_user.domain.Watermeter;
import com.woniuxy.threeproject.end_water_project_user.domain.form.UserForm;
import com.woniuxy.threeproject.end_water_project_user.service.IAccountService;
import com.woniuxy.threeproject.end_water_project_user.service.IUserService;
import com.woniuxy.threeproject.end_water_project_user.service.IWatermeterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@RestController
@Slf4j
@RequestMapping("/user")
@CrossOrigin("*")
public class UserController {
    @Resource
    public UserMapper userMapper;

    @Resource
    public IUserService userService;
    @Resource
    public IAccountService iAccountService;
    @Resource
    public IWatermeterService iWatermeterService;

    @GetMapping("login")
    public String addUser(UserForm uf){
        log.info("开始接受数据："+uf);
        User u = new User();
        BeanUtils.copyProperties(uf,u);
        log.info("{}",u);
        int i = userMapper.insert(u);
        if(i>0){
            return "success";
        }else{
            return "fail";
        }
    }
    @GetMapping("waterQuery")
    public Result findWater(String card,String idCard){
        log.info("水费查询，接收用户请求，{} {}",card,idCard);
        UserChekeUtile.cheke(card,idCard);
        Result queryUser = userService.findQueryUser(card, idCard);
        return queryUser;
    }
    @GetMapping("paymentForm")
    public Result findPaymentForm(String card,String idCard){
        log.info("缴费查询，接收用户请求，{} {}",card,idCard);
        UserChekeUtile.cheke(card,idCard);
        Result paymentForm = userService.findPaymentForm(card, idCard);
        return paymentForm;
    }

//    开始用户开表(管理人员注册表)
    @GetMapping("kb")
    public Result Kb(String name,String address,Long phone,String idCard){
        log.info("name={},address={},phone={},idCard={}",name,address,phone,idCard);
//        得到用户id，自动生成，自增长
// ----------------新增账号表------------------------

        Account ac= AccountAndWaterMeter.getAccount();
        Account reac = iAccountService.addAccount(ac);
        if(null!=reac){
           log.info("注册账户表成功！");
       }else{
           log.warn("注册账户表失败！");
           return Result.fail("4007","服务器开小差了！");
       }
       //开始注册水表
        Watermeter rewater = createWaterMeter();

//--------- 开始注册用户表---------------------------
//         卡号自动生成
//        int n = new Random().nextInt(1000);
//        String card=UUIDUtils.getUUid().toString().replace("-","").substring(0,4)+n;
////        状态自动生成 默认为不供水0
////        水表id后端生成
////        开户日期默认为添加时间
//
//        Integer accountId=reac.getAccountId();
//        Integer waterMeterId=rewater.getWaterMeterId();
//        User u = new User();
//        u.setUserName(name);
//        u.setAddress(address);
//        u.setPhone(phone);
//        u.setIdCard(idCard);
//        u.setCard(card);
//        u.setWaterMeterId(waterMeterId);
//        u.setAccountId(accountId);
        User u=AccountAndWaterMeter.getUser(name,address,idCard,phone);
        u.setWaterMeterId(rewater.getWaterMeterId());
        u.setAccountId(reac.getAccountId());
        User ru= createUser(u);
        if(null!=ru){
            log.info("新增水表成功！");
            return  Result.success("200","新增水表成功！");
        }else{
            log.warn("新增水表失败！");
            return  Result.fail("4006","新增水表失败！");
        }

    }
//    新增用户表
    private User createUser(User u) {
        log.info("controller开始新增用户表");
      User ru=  userService.addUser(u);
      return ru;

    }
//    新增水表
    private Watermeter createWaterMeter() {
        Watermeter wm = AccountAndWaterMeter.getWaterMeter();
        Watermeter watermeter=iWatermeterService.addWaterMeter(wm);
        if(null!=watermeter){
            log.info("新增水表成功！");
            return  watermeter;
        }else{
            log.warn("新增水表失败！");
            return  null;
        }



    }

}

